package com.example.lenovo.appstore.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.activity.PurchasedItemsActivity;
import com.example.lenovo.appstore.activity.RechargeActivity;
import com.example.lenovo.appstore.activity.SettingActivity;
import com.example.lenovo.appstore.activity.UserInfoActivity;
import com.example.lenovo.appstore.utils.AppContext;
import com.example.lenovo.appstore.utils.StatusBarUtil;
import com.example.lenovo.appstore.view.TitleBarView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：AppStore
 *
 * @Author 郭迎辉
 * Create by 9:39 on 2019/3/20.
 * Describe:
 */
public class PersonalFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.index_titleBar)
    TitleBarView titleBarView;
    @Bind(R.id.user)
    RelativeLayout user;

    @Bind(R.id.PurchasedItems)
    RelativeLayout PurchasedItems;
    @Bind(R.id.chongzhi)
    RelativeLayout chongzhi;
    @Bind(R.id.setting)
    RelativeLayout setting;

    @Bind(R.id.logout)
    TextView logout;//退出
    @Bind(R.id.setting_tv)
    TextView setting_tv;//设置
    @Bind(R.id.tv_yigou)
    TextView yigou;//已购
    @Bind(R.id.tv_chongzhi)
    TextView chongzhi_tv;//充值

    public static PersonalFragment instance;
    private Context context;
    private View view;
    public static PersonalFragment getInstance(){
        if(null==instance){
            instance=new PersonalFragment();
        }
        return instance;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (null == view) {
            view = inflater.inflate(R.layout.fragment_index, container, false);
            ButterKnife.bind(this, view);
            initView();
        } else {
            if (null != view.getParent()) {
                ((ViewGroup) view.getParent()).removeAllViews();
            }

        }
        instance = this;
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 隐藏软键盘
        getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        StatusBarUtil.setStatusBarLightMode(getActivity().getWindow());
    }

    private void initView() {
        logout.setText(AppContext.getInstance().getResources().getString(R.string.logout));

        chongzhi_tv.setText(AppContext.getInstance().getResources().getString(R.string.chongzhi));
        setting_tv.setText(AppContext.getInstance().getResources().getString(R.string.Setup));
        titleBarView.setTitle(AppContext.getInstance().getResources().getString(R.string.center));
        yigou.setText(AppContext.getInstance().getResources().getString(R.string.Purchaseditems));
        titleBarView.showBackButton(false);
        titleBarView.showEditButton(true);
        PurchasedItems.setOnClickListener(this);
        chongzhi.setOnClickListener(this);
        user.setOnClickListener(this);
        setting.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.chongzhi:
                Intent recharge = new Intent(getActivity(), RechargeActivity.class);
                startActivity(recharge);
                break;
            case R.id.PurchasedItems:
                startActivity(new Intent(getActivity(), PurchasedItemsActivity.class));
                break;
            case R.id.user:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.setting:
                Intent setting=new Intent(getActivity(),SettingActivity.class);
                startActivity(setting);
                break;
        }
    }
}
